package com.mvpandroiddemo.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.mvpandroiddemo.MvpApplication;


/**
 * @Description $description$
 * @Author owner
 * @CreateTime 2019/10/15 13:52
 **/
public class SharedPreferenceHelper {

    private volatile static SharedPreferenceHelper sharedPreferenceHelper = null;

    private static SharedPreferences sharedPreferences;

    private SharedPreferenceHelper() {

    }

    public static SharedPreferenceHelper getInstance() {
        if (sharedPreferenceHelper == null) {
            synchronized (SharedPreferenceHelper.class) {
                sharedPreferenceHelper = new SharedPreferenceHelper();
            }
        }
        if (sharedPreferences == null) {
            sharedPreferences = MvpApplication.getInstance().getSharedPreferences(Constance.Cache_Project_Name, Context.MODE_PRIVATE);
        }
        return sharedPreferenceHelper;
    }


    public <T> void saveData(String key, T value) {
        if (value instanceof String) {
            sharedPreferences.edit().putString(key, String.valueOf(value)).apply();
        }

        if (value instanceof Integer) {
            sharedPreferences.edit().putInt(key, ((Integer) value)).apply();
        }
        if (value instanceof Boolean) {
            sharedPreferences.edit().putBoolean(key, (Boolean) value).apply();

        }
    }

    public String getString(String key) {
        return sharedPreferences.getString(key, "");
    }


    public boolean getBoolean(String key) {
        return sharedPreferences.getBoolean(key, false);
    }


}
